/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * buscarMedicamento.java
 *
 * Created on 01-may-2011, 10:59:55
 */

package interfaces.farmacia;

import Controlador.Controlador;
import Definiciones.Definiciones;
import GestorFarmacia.Medicamento;
import clinica.ClinicaView;
import clinica.Interfaces;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import clinica.Interfaz;
import java.text.ParseException;
import org.jdesktop.application.Action;
/**
 *
 * @author Ana Ferreira Blanco
 */
public class buscarMedicamento extends javax.swing.JPanel {

    private ClinicaView app;

    /** Creates new form buscarMedicamento */
    public buscarMedicamento(ClinicaView a) {
        app = a;
        initComponents();
        //No dejes cosas que no van, esta funcion no recive parametros
        //Controlador.setFontSize(label_nombre);
        //Controlador.setFontSize(label_via);
        //Controlador.setFontSize(label_atc);
        //Controlador.setFontSize(label_dci);
    }

    private void limpiar() {
        nombre.setText("");
        dci_marca.setText("");
        desplegable1.setSelectedIndex(0);
        desplegable2.setSelectedIndex(0);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        nombre = new javax.swing.JTextField();
        label_nombre = new javax.swing.JLabel();
        label_atc = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        desplegable1 = new javax.swing.JComboBox();
        label_dci = new javax.swing.JLabel();
        dci_marca = new javax.swing.JTextField();
        label_via = new javax.swing.JLabel();
        desplegable2 = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        cancelar = new javax.swing.JButton();
        aceptar = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(780, 470));
        setPreferredSize(new java.awt.Dimension(780, 470));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize()));
        jPanel1.setFont(Interfaz.setFontSize());
        jPanel1.setName("jPanel1"); // NOI18N

        nombre.setFont(Interfaz.setFontSize());
        nombre.setName("nombre"); // NOI18N

        label_nombre.setFont(Interfaz.setFontSize());
        label_nombre.setText("Nombre");
        label_nombre.setName("label_nombre"); // NOI18N

        label_atc.setFont(Interfaz.setFontSize());
        label_atc.setText("Categoría ATC");
        label_atc.setName("label_atc"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 52, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 308, Short.MAX_VALUE)
        );

        jPanel3.setName("jPanel3"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 79, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 308, Short.MAX_VALUE)
        );

        desplegable1.setFont(Interfaz.setFontSize());
        desplegable1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TODAS", "A", "B", "C", "D", "G", "H", "J", "L", "M", "N", "P", "R", "S", "V" }));
        desplegable1.setName("desplegable1"); // NOI18N

        label_dci.setFont(Interfaz.setFontSize());
        label_dci.setText("DCI o marca");
        label_dci.setName("label_dci"); // NOI18N

        dci_marca.setFont(Interfaz.setFontSize());
        dci_marca.setName("dci_marca"); // NOI18N

        label_via.setFont(Interfaz.setFontSize());
        label_via.setText("Via Administración");
        label_via.setName("label_via"); // NOI18N

        desplegable2.setFont(Interfaz.setFontSize());
        desplegable2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TODAS", "OFTALMICA", "OPTICA", "NASAL", "ORAL", "ORALAREOSOL", "TOPICA", "PARENTERAL", "RECTAL", "VAGINAL", "OTRA" }));
        desplegable2.setName("desplegable2"); // NOI18N

        jPanel4.setName("jPanel4"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 728, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 6, Short.MAX_VALUE)
        );

        jPanel5.setName("jPanel5"); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 375, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 4, Short.MAX_VALUE)
        );

        jPanel6.setName("jPanel6"); // NOI18N

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 585, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 87, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(97, 97, 97)
                                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(label_nombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(label_atc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(label_dci, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(1, 1, 1)
                                        .addComponent(label_via, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(dci_marca)
                                    .addComponent(desplegable2, 0, 296, Short.MAX_VALUE)
                                    .addComponent(nombre, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                                    .addComponent(desplegable1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label_nombre)
                            .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label_atc)
                            .addComponent(desplegable1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label_dci)
                            .addComponent(dci_marca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label_via, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(desplegable2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(buscarMedicamento.class, this);
        cancelar.setAction(actionMap.get("cancelar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(buscarMedicamento.class);
        cancelar.setIcon(resourceMap.getIcon("cancelar.icon")); // NOI18N
        cancelar.setLabel(resourceMap.getString("cancelar.label")); // NOI18N
        cancelar.setName("cancelar"); // NOI18N

        aceptar.setAction(actionMap.get("aceptar")); // NOI18N
        aceptar.setIcon(resourceMap.getIcon("aceptar.icon")); // NOI18N
        aceptar.setLabel(resourceMap.getString("cancelar.label")); // NOI18N
        aceptar.setName("aceptar"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cancelar)
                        .addGap(18, 18, 18)
                        .addComponent(aceptar)
                        .addGap(8, 8, 8))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(aceptar)
                    .addComponent(cancelar))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    @Action
    public void aceptar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException, ParseException {
        Definiciones.ViaAdministracion via = null;
        Definiciones.ATC categoria = null;
        String name=null, dci=null;

        if(desplegable1.getSelectedIndex()==0)
            categoria = null;
        else if(desplegable1.getSelectedIndex()==1)
            categoria = Definiciones.ATC.A;
        else if(desplegable1.getSelectedIndex()==2)
            categoria = Definiciones.ATC.B;
        else if(desplegable1.getSelectedIndex()==3)
            categoria = Definiciones.ATC.C;
        else if(desplegable1.getSelectedIndex()==4)
            categoria = Definiciones.ATC.D;
        else if(desplegable1.getSelectedIndex()==5)
            categoria = Definiciones.ATC.G;
        else if(desplegable1.getSelectedIndex()==6)
            categoria = Definiciones.ATC.H;
        else if(desplegable1.getSelectedIndex()==7)
            categoria = Definiciones.ATC.J;
        else if(desplegable1.getSelectedIndex()==8)
            categoria = Definiciones.ATC.L;
        else if(desplegable1.getSelectedIndex()==9)
            categoria = Definiciones.ATC.M;
        else if(desplegable1.getSelectedIndex()==10)
            categoria = Definiciones.ATC.N;
        else if(desplegable1.getSelectedIndex()==11)
            categoria = Definiciones.ATC.P;
        else if(desplegable1.getSelectedIndex()==12)
            categoria = Definiciones.ATC.R;
        else if(desplegable1.getSelectedIndex()==13)
            categoria = Definiciones.ATC.S;
        else if(desplegable1.getSelectedIndex()==14)
            categoria = Definiciones.ATC.V;

        if(desplegable2.getSelectedIndex()==0)
            via = null;
        else if(desplegable2.getSelectedIndex() == 1)
            via = Definiciones.ViaAdministracion.OFTALMICA;
        else if(desplegable2.getSelectedIndex()==2)
            via = Definiciones.ViaAdministracion.OPTICA;
        else if(desplegable2.getSelectedIndex()==3)
            via = Definiciones.ViaAdministracion.NASAL;
        else if(desplegable2.getSelectedIndex()==4)
            via = Definiciones.ViaAdministracion.ORAL;
        else if(desplegable2.getSelectedIndex()==5)
            via = Definiciones.ViaAdministracion.ORALAREOSOL;
        else if(desplegable2.getSelectedIndex()==6)
            via = Definiciones.ViaAdministracion.TOPICA;
        else if(desplegable2.getSelectedIndex()==7)
            via = Definiciones.ViaAdministracion.PARENTERAL;
        else if(desplegable2.getSelectedIndex()==8)
            via = Definiciones.ViaAdministracion.RECTAL;
        else if(desplegable2.getSelectedIndex()==9)
            via = Definiciones.ViaAdministracion.VAGINAL;
        else if(desplegable2.getSelectedIndex()==10)
            via = Definiciones.ViaAdministracion.OTRA;

        System.out.println("N: "+nombre.getText());
        if(!nombre.getText().equals(""))
            name = nombre.getText();
        if(!dci_marca.getText().equals(""))
             dci = dci_marca.getText();
        
        Medicamento m = new Medicamento(null,categoria, name,dci,via,null, null, null, null);
        limpiar();
        app.abrirBuscarMedicamento2(m, Interfaces.PRINCIPAL_FARMACEUTICO);
    }
    
    @Action
    public void cancelar() {
        limpiar();
        app.volver(Interfaces.PRINCIPAL_FARMACEUTICO);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptar;
    private javax.swing.JButton cancelar;
    private javax.swing.JTextField dci_marca;
    private javax.swing.JComboBox desplegable1;
    private javax.swing.JComboBox desplegable2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JLabel label_atc;
    private javax.swing.JLabel label_dci;
    private javax.swing.JLabel label_nombre;
    private javax.swing.JLabel label_via;
    private javax.swing.JTextField nombre;
    // End of variables declaration//GEN-END:variables

}
